// BmarkKeyGen.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>

#define MIN_KEYS	200
#define MAX_KEYS	10000

#define KEY_LENGTH	25

void GenerateKey(int* key)
{
	BOOL isSet[4];
	isSet[0] = FALSE;
	isSet[1] = FALSE;
	isSet[2] = FALSE;
	isSet[3] = FALSE;
	key[0] = key[1] = key[2] = key[3] = 0;
	do
	{
		int randInt = (rand() % 26112) + 0x1a00;
		if (randInt % 4 == 0 && randInt >= 0x1a00
			&& (key[0] <= 0x1a00 || key[0] > 0x7fff))
		{
			key[0] = randInt;
			isSet[0] = TRUE;
		}
		else if (randInt % 4 == 1 && randInt >= 0x1a00
			&& (key[1] <= 0x1a00 || key[1] > 0x7fff))
		{
			key[1] = randInt;
			isSet[1] = TRUE;
		}
		else if (randInt % 4 == 0 && randInt >= 0x1a00
			&& (key[2] <= 0x1a00 || key[2] > 0x7fff))
		{
			key[2] = randInt;
			isSet[2] = TRUE;
		}
		else if (randInt % 4 == 1 && randInt >= 0x1a00
			&& (key[3] <= 0x1a00 || key[3] > 0x7fff))
		{
			key[3] = randInt;
			isSet[3] = TRUE;
		}
	} while(!isSet[0] || !isSet[1] || !isSet[2] || !isSet[3]);
}

int _tmain(int argc, _TCHAR* argv[])
{
	char szKeyBuf[KEY_LENGTH];
	int numKeys = MIN_KEYS;
	printf("Dacris Benchmarks 6.0 Key Generator\n");
	printf("(C) 2016 Dacris Software Inc. All Rights Reserved.\n");
	printf("\n");
	printf("Enter number of keys to generate:\n");
	scanf_s("%d", &numKeys);
	if (numKeys < MIN_KEYS || numKeys > MAX_KEYS)
	{
		printf("Number of keys must be between 200 and 10000.");
		return 1;
	}
	HANDLE hFile = CreateFile("Keys.txt", GENERIC_WRITE,
			FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		printf("Error writing to file 'Keys.txt'.");
		return 1;
	}
	for (int i = 0; i < numKeys; i++)
	{
		int key[4];
		GenerateKey(key);
		wsprintf(szKeyBuf, "%04x-%04x-%04x-%04x\r\n", key[0], key[1], key[2], key[3]);
		DWORD dwBytesWritten;
		WriteFile(hFile, szKeyBuf, lstrlen(szKeyBuf), &dwBytesWritten, NULL);
	}
	CloseHandle(hFile);
	return 0;
}

